import requests,json,time

# 定义openstack api地址和认证信息
controller_ip = "192.168.95.31"
# home eve allinone



#controller_ip = "192.168.88.49"
domain = "domain"
username = "admin"
password = "000000"

# 定义各组件api地址
keystoneurl = f"http://"+controller_ip+":5000/v3/auth/tokens"
flavorurl = f"http://"+controller_ip+":8774/v2.1/flavors"
imageurl = f"http://"+controller_ip+":9292/v2/images"
networkurl = f"http://"+controller_ip+":9696/v2.0/networks"
subneturl = f"http://"+controller_ip+":9696/v2.0/subnets"

# 获取token
def getToken():
        try:
            url = keystoneurl

            body = {"auth": {"identity": {"methods": ["password"], "password": {
                    "user": {"domain": {"name": domain }, "name": username, "password": password}}},
                       "scope": {"project": {"domain": {"name": domain }, "name": "admin"}}}}

            headers = {
                        "Content-Type": "application/json",
                    }

            Token = requests.post(url, data=json.dumps(body), headers=headers).headers['X-Subject-Token']

            headers = {
                        "X-Auth-Token": Token
                    }

            return headers

        except Exception as e:
            print(f"获取Token值失败，请检查访问云主机控制节点IP是否正确？输出错误信息如下：{str(e)}")
